{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from PyQt4 import QtGui\n",
    "import os, sys\n",
    "import pandas as pd\n",
    "import pandas_datareader.data as web\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas\n",
    "from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2009-03-16</th>\n",
       "      <td>162.83</td>\n",
       "      <td>164.70</td>\n",
       "      <td>159.14</td>\n",
       "      <td>159.69</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-03-17</th>\n",
       "      <td>159.93</td>\n",
       "      <td>167.50</td>\n",
       "      <td>159.39</td>\n",
       "      <td>167.50</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-03-18</th>\n",
       "      <td>167.24</td>\n",
       "      <td>169.83</td>\n",
       "      <td>163.86</td>\n",
       "      <td>166.38</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-03-19</th>\n",
       "      <td>165.67</td>\n",
       "      <td>167.83</td>\n",
       "      <td>163.53</td>\n",
       "      <td>164.81</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-03-20</th>\n",
       "      <td>164.98</td>\n",
       "      <td>166.33</td>\n",
       "      <td>163.01</td>\n",
       "      <td>164.91</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Open    High     Low   Close  Volume\n",
       "Date                                              \n",
       "2009-03-16  162.83  164.70  159.14  159.69     NaN\n",
       "2009-03-17  159.93  167.50  159.39  167.50     NaN\n",
       "2009-03-18  167.24  169.83  163.86  166.38     NaN\n",
       "2009-03-19  165.67  167.83  163.53  164.81     NaN\n",
       "2009-03-20  164.98  166.33  163.01  164.91     NaN"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "google = web.DataReader('GOOG', data_source = 'google',start = '3/14/2009', end = '4/14/2016')\n",
    "#DataReader('GOOG',  start = '3/14/2009', end = '4/14/2016')\n",
    "google.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2009-03-16</th>\n",
       "      <td>162.83</td>\n",
       "      <td>164.70</td>\n",
       "      <td>159.14</td>\n",
       "      <td>159.69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-03-17</th>\n",
       "      <td>159.93</td>\n",
       "      <td>167.50</td>\n",
       "      <td>159.39</td>\n",
       "      <td>167.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-03-18</th>\n",
       "      <td>167.24</td>\n",
       "      <td>169.83</td>\n",
       "      <td>163.86</td>\n",
       "      <td>166.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-03-19</th>\n",
       "      <td>165.67</td>\n",
       "      <td>167.83</td>\n",
       "      <td>163.53</td>\n",
       "      <td>164.81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-03-20</th>\n",
       "      <td>164.98</td>\n",
       "      <td>166.33</td>\n",
       "      <td>163.01</td>\n",
       "      <td>164.91</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Open    High     Low   Close\n",
       "Date                                      \n",
       "2009-03-16  162.83  164.70  159.14  159.69\n",
       "2009-03-17  159.93  167.50  159.39  167.50\n",
       "2009-03-18  167.24  169.83  163.86  166.38\n",
       "2009-03-19  165.67  167.83  163.53  164.81\n",
       "2009-03-20  164.98  166.33  163.01  164.91"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "google = google.drop('Volume', axis = 1 )\n",
    "google.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class PrettyWidget(QtGui.QWidget):\n",
    "    \n",
    "    \n",
    "    def __init__(self):\n",
    "        super(PrettyWidget, self).__init__()\n",
    "        self.initUI()\n",
    "        \n",
    "        \n",
    "    def initUI(self):\n",
    "        self.setGeometry(600,300, 1000, 600)\n",
    "        self.center()\n",
    "        self.setWindowTitle('Revision on Plots, Tables and File Browser')     \n",
    "        \n",
    "        #Grid Layout\n",
    "        grid = QtGui.QGridLayout()\n",
    "        self.setLayout(grid)            \n",
    "        #Canvas and Toolbar\n",
    "        self.figure = plt.figure(figsize=(15,5))    \n",
    "        self.canvas = FigureCanvas(self.figure)     \n",
    "        self.toolbar = NavigationToolbar(self.canvas, self)\n",
    "        grid.addWidget(self.canvas, 2,0,1,2)\n",
    "        grid.addWidget(self.toolbar, 1,0,1,2)\n",
    "        \n",
    "        #Import CSV Button\n",
    "        btn1 = QtGui.QPushButton('Import CSV', self)\n",
    "        btn1.resize(btn1.sizeHint()) \n",
    "        btn1.clicked.connect(self.getCSV)\n",
    "        grid.addWidget(btn1, 0,0)\n",
    "        \n",
    "        #Plot Button\n",
    "        btn2 = QtGui.QPushButton('Plot', self)\n",
    "        btn2.resize(btn2.sizeHint())    \n",
    "        btn2.clicked.connect(self.plot)\n",
    "        grid.addWidget(btn2, 0,1)\n",
    "    \n",
    "        self.show()\n",
    "    \n",
    "    \n",
    "    def getCSV(self):\n",
    "        filePath = QtGui.QFileDialog.getOpenFileName(self, \n",
    "                                                       'Single File',\n",
    "                                                       '~/Desktop/PyRevolution/PyQt4',\n",
    "                                                       '*.csv')\n",
    "        fileHandle = open(filePath, 'r')\n",
    "        line = fileHandle.readline()[:-1].split(',')\n",
    "        for n, val in enumerate(line):\n",
    "            newitem = QtGui.QTableWidgetItem(val)\n",
    "            self.table.setItem(0, n, newitem)\n",
    "        self.table.resizeColumnsToContents()\n",
    "        self.table.resizeRowsToContents()    \n",
    "    \n",
    "    \n",
    "    def plot(self):\n",
    "        y = []\n",
    "        for n in range(9):\n",
    "            try:\n",
    "                y.append(float(self.table.item(0, n).text()))\n",
    "            except:\n",
    "                y.append(np.nan)\n",
    "        plt.cla()\n",
    "        ax = self.figure.add_subplot(111)\n",
    "        ax.plot(y, 'r.-')\n",
    "        ax.set_title('Table Plot')\n",
    "        self.canvas.draw()\n",
    "    \n",
    "    \n",
    "    def center(self):\n",
    "        qr = self.frameGeometry()\n",
    "        cp = QtGui.QDesktopWidget().availableGeometry().center()\n",
    "        qr.moveCenter(cp)\n",
    "        self.move(qr.topLeft())\n",
    "    \n",
    "    \n",
    "        \n",
    "def main():\n",
    "    app = QtGui.QApplication(sys.argv)\n",
    "    w = PrettyWidget()\n",
    "    app.exec_()\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
